# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Sum, Ref
from ..base import OpsFeature

from .close import Close

class ROC(OpsFeature):
    """Technical Indicator - ROC

    - ROC = (CLOSE - REF(CLOSE, N)) / REF(CLOSE, N) * 100
    """
    def __init__(self, feature=None, N=12):
        self.feature = feature or Close()
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N != 12:
            name += '_'+str(self.N)
        return name

    def _load_internal(self, instrument, start_time, end_time, freq):
        roc = (self.feature - Ref(self.feature, self.N)) / Ref(self.feature, self.N) * 100
        return roc.load(instrument, start_time, end_time, freq)
